/*
 * Copyright (c) 2012 Universidad Antonio de Nebrija.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0.html
 * 
 * Contributors:
 *    Universidad Antonio de Nebrija - initial API, implementation and documentation
 */
package com.nebrija.riif.cli;

import java.util.ArrayList;

import com.nebrija.riif.model.RootClass;
import com.nebrija.riif.model.StepTimelineExecution;
import com.nebrija.riif.model.TimeStrings;
import com.nebrija.riif.model.Timeline;
import com.nebrija.riif.model.ValueTimeUnit;

/**
 * Command to resolve the cross-references in the property values
 * @author asanchep
 *
 */
public class StepTimelineCommand implements Command {

	@Override
	public String execute(ArrayList<String> params) throws CommandException {
		if(params.size()<2){
			return "Missing number of units or unit used";
		}
		RootClass r = RootClass.getInstance();
		Timeline t = r.getGlobalTimeline();
		if (t==null || t.getExecution() == null || !(t.getExecution() instanceof StepTimelineExecution)){
			return "A valid timeline must be set as global with an associated StepTimelineExecution option";
		}
		StepTimelineExecution exec = (StepTimelineExecution)t.getExecution();
		try{
			TimeStrings.valueOf(params.get(1));
		}catch(Exception e){
			return "Wrong unit used";
		}
		return exec.step(ValueTimeUnit.stringToVTU(""+params.get(0)+params.get(1)));
	
	}

	@Override
	public String helpString() throws CommandException {
		return " <int> <unit>: advance a timeline an int number of units (ms, s, mins, hours, months, years)";
	}

}
